﻿using EazyCMS.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using EazyCMS.Common;
using EazyCMS.Model;

namespace EazyCMS.Web
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {
            tools_helper bll = new tools_helper();
            visit_num ipinfo = new visit_num();
            //获取访问者的IP   
            string ipAddress = Request.ServerVariables["REMOTE_ADDR"];
            //获取访问时间   
            DateTime ipDatetime = DateTime.Now;
            ipinfo.iPadress = ipAddress;
            ipinfo.logintime = ipDatetime;
            //首先清除前一天的ip数据留下当天的
            //得到当前时间的前一天
            DateTime pretime = DateTime.Now.AddDays(-1);
            int year = pretime.Year;
            int month = pretime.Month;
            int day = pretime.Day;
            DateTime dtstar = Convert.ToDateTime(year + "-" + month + "-" + day + " 00:00:01");
            DateTime dtend = Convert.ToDateTime(year + "-" + month + "-" + day + " 23:59:59");
            string todayKey = DateTime.Now.ToString("yyyyMMdd");
            object biaoshi = CacheHelper.Get(todayKey);
            if (biaoshi == null)
            {
                CacheHelper.Insert(todayKey, "cunzai");
                string yesterdayKey = DateTime.Now.AddDays(-1).ToString("yyyyMMdd");

                if (CacheHelper.Get(yesterdayKey) != null)
                {
                    CacheHelper.Remove(yesterdayKey);
                }
                ///清除昨天的ip地址记录
                /// 
                bll.delete_visit_all(dtstar, dtend);
            }

            //判断该ip地址是否在数据库中变量i>0表示存在数据库中，反之不存在数据库中直接将改地址插入数据库
            int i = bll.select_visit_byip(ipAddress);
            if (i > 0)
            {

            }
            else
            {
                //将该IP地址相关信息插入到数据库中
                bll.insert_visit_num(ipinfo);
                bll.updatenum();

            }

        }


        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}